<%@ include file="/WEB-INF/jsp/include.jsp"  %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<c:set var="page_title"  value="view dish recipe"/>
<html>
<head>
    <title><c:out value="${page_title}"/></title>
    <c:url var="jquery_custom_css" value="/ui/jquery-ui-1.8.4.custom.css" />
    <link href="${jquery_custom_css}" rel="stylesheet" type="text/css" />
    <c:url var="jquery_treeview_css" value="/css/jquery.treeview.css" />
    <link href="${jquery_treeview_css}" rel="stylesheet" type="text/css" />
    <c:url var="housewifes_css" value="/css/housewifes.css" />
    <link href="${housewifes_css}" rel="stylesheet" type="text/css" />

    <c:url var="jquery_js" value="/script/jquery-1.4.2.js" />
    <script type="text/javascript" src="${jquery_js}"></script>
    <c:url var="jquery_cookie_js" value="/script/jquery.cookie.js" />
    <script type="text/javascript" src="${jquery_cookie_js}"></script>
    <c:url var="jquery_treeview_js" value="/script/jquery.treeview.js" />
    <script type="text/javascript" src="${jquery_treeview_js}"></script>
    <c:url var="jquery_custom_js" value="/script/jquery-ui-1.8.4.custom.min.js" />
    <script type="text/javascript" src="${jquery_custom_js}"></script>

    <c:url var="hw_js" value="/script/housewifes.js" />
    <script type="text/javascript" src="${hw_js}"></script>

</head>
<body>
<c:import url="../pagemenus/mainUserMenu.jsp"/>
<br/>
<h3><c:out value="${page_title}"/></h3>

<table>
    <tr>
        <td>name</td>
        <td>
            <c:out value="${recipe.name}"/>
        </td>
    </tr>
    <tr>
        <td>wayToPrepare</td>
        <td>
            <c:out value="${recipe.wayToPrepare}"/>
        </td>
    </tr>
    <tr>
        <td>group</td>
        <td>
            <c:out value="${recipe.group.name}"/>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            components:
            <table border="1">
                <tr>
                    <th>ingredient</th>
                    <c:choose>
                        <c:when test="${!isUsersRecipe}">
                    <th>authors preferable ware</th>
                    <th>my preferable ware</th>
                        </c:when>
                        <c:otherwise>
                    <th>my(authors) preferable ware</th>
                        </c:otherwise>
                    </c:choose>
                    <th>count</th>
                </tr>
                <c:forEach var="component" items="${recipe.components}">
                    <tr>
                        <td>
                            <c:out value="${component.ingredient.name}"/>
                        </td>
                        <td>
                            <c:choose>
                                <c:when test="${component.preferableWare != null}">
                                    <c:set var="cpWare" value="${component.preferableWare}"/>
                                </c:when>
                                <c:otherwise>
                                    <c:set var="cpWare" value="${component.ingredient.defaultWare}"/>
                                </c:otherwise>
                            </c:choose>
                            <c:set var="userWare" value="${usersWaresMap[cpWare.id]}"/>
                            <c:out value="${cpWare.name}"/>
                            <c:if test="${userWare != null}">
                                (<c:out value="${userWare.wareRegionPriceStrValue}"/> /
                                <span class="dialog_link select_user_price">
                                    <span class="hidden_id">${userWare.id}</span>
                                    <span class="hidden_span ware_name">${cpWare.name}</span>
                                    <span class="users_ware_price_${userWare.id}"><c:out value="${userWare.wareUserPriceStrValue}"/></span>
                                </span>
                                )
                            </c:if>
                        </td>
                        <c:if test="${!isUsersRecipe}">
                        <td>
                            <c:set var="usersPreferableWare" value="${preferableWaresForComponents[component.id]}"/>
                            <c:set var="userWare" value="${usersPreferableWare != null ? usersWaresMap[usersPreferableWare.ware.id] : null}"/>
                            <span id="pref_ware_${component.id}" class="<c:if test="${usersPreferableWare == null}">hidden_span</c:if>">
                                <span class="ware_name"><c:if test="${usersPreferableWare != null}"><c:out value="${usersPreferableWare.ware.name}"/></c:if></span>
                                (
                                <span class="region_price"><c:out value="${userWare != null ? userWare.wareRegionPriceStrValue : '-'}"/></span> /
                                <span class="dialog_link select_user_price">
                                    <span class="hidden_id">${(userWare != null) ? userWare.id : ""}</span>
                                    <span class="hidden_span ware_name">${userWare != null ? userWare.ware.name : ""}</span>
                                    <span class="users_ware_price_${userWare != null ? userWare.id : ""}"><c:out value="${userWare != null ? userWare.wareUserPriceStrValue : '-'}"/></span>
                                </span>
                                )
                            </span>
                            <span class="dialog_link select_pref_ware">
                                <span class="hidden_id ingred_id">${component.ingredient.id}</span>
                                <span class="hidden_id comp_id">${component.id}</span>
                                select your ware
                            </span>
                        </td>
                        </c:if>
                        <td>
                            <c:out value="${component.count}"/>
                            &nbsp;
                            <c:out value="${component.measure}"/>
                        </td>
                    </tr>
                </c:forEach>
            </table>
        </td>
    </tr>
</table>

<div id="dialog_container1" style="display:none;">
    <div id="wares_select_dialog">loading wares groups...</div>
</div>

<div id="loading_dialog" style="display:none;">
    <img src="img/loading.gif" style="text-align:center;" width="100%" height="100%" />
</div>

<c:import url="../prices/setUsersWarePriceDialogBlock.jsp"/>

<script type="text/javascript">

    $(document).ready(function() {
        var wsd = new WaresSelectDialog("wares_select_dialog", "<c:url value="/get-select-wares-form.html"/>", "<c:url value="/get-wares-list.html"/>");

        $(".select_pref_ware").each(function() {
            var ingredId = $(this).children(".ingred_id").text();
            var compId = $(this).children(".comp_id").text();
            $(this).click(function() {
                wsd.onWareDialogLinkClick(ingredId, function(wareView) {
                    var wareId = wareView['ware']['id'];
                    var wareName = wareView['ware']['name'];
                    var regionPrice = wareView['regionPrice'];
                    showLoadingDialog();
                    $.ajax({
                        url: "<c:url value="/set-preferable-ware-for-recipes-component.html"/>" ,
                        type: 'GET',
                        dataType: 'json',
                        data: {
                            "usersTableId": ${usersTableId},
                            "dayMenuId": ${dayMenuId},
                            "eatingMenuId": ${eatingMenuId},
                            "recipeId": ${recipe.id},
                            "componentId": compId,
                            "wareId": wareId
                        },
                        success: function(reply) {
                            if (reply) {
                                if (reply["result"] == "OK") {

                                    var prefWareSpan = $("#pref_ware_" + compId);

                                    var prevWareId = prefWareSpan.find(".select_user_price").find(".hidden_id").text();

                                    prefWareSpan.find(".ware_name").html(wareName);
                                    prefWareSpan.find(".region_price").html(regionPrice);
                                    prefWareSpan.find(".select_user_price").find(".hidden_id").html(wareId);
                                    var usersWarePriceSpan = prefWareSpan.find(".select_user_price").find(".users_ware_price_" + prevWareId);
                                    usersWarePriceSpan.removeClass("users_ware_price_" + prevWareId);
                                    usersWarePriceSpan.addClass("users_ware_price_" + wareId);
                                    //usersWarePriceSpan.html("TBD");
                                    usersWarePriceSpan.html(reply["usersWarePrice"]);

                                    prefWareSpan.removeClass("hidden_span");
                                }
                                hideLoadingDialog();
                            } else {
                                alert("server error");
                                window.location.reload(true);
                            }
                        },
                        error: function(error) {
                            alert(error);
                            window.location.reload(true);
                        }
                    });

                });
            });

        });

        //--- wares prices manage:
        var priceDialog = new WareUserPriceSetDialog(
                "<c:url value="/set-users-ware-price.html"/>",
                "set_users_ware_price_form",
                "set_price_form_ware_id",
                "set_price_form_ware_name",
                "set_price_form_ware_price"
        );

        priceDialog.bindSubmitPriceAction(function(wareId, warePrice) {
            $(".users_ware_price_" + wareId).html(warePrice);
        });

        $(".select_user_price").each(function() {
            var selectUserPricePointer = $(this);
            $(this).click(function() {
                var wareId = selectUserPricePointer.children(".hidden_id").text();
                var wareName = selectUserPricePointer.children(".ware_name").text();
                var wareCurPriceSpan = selectUserPricePointer.children(".users_ware_price_" + wareId);
                priceDialog.showDialogForm(wareId, wareName, function(){
                    var wareCurPrice = wareCurPriceSpan.text();
                    if (wareCurPrice == "<spring:message code="ware.user.price.not.found"/>") {
                        wareCurPrice = 0;
                    }
                    return wareCurPrice;
                });
            });
        });

    });
</script>

</body>
</html>